package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.MainPageDao;
import cn.topevery.garden.entity.oneMap.CountVO;
import cn.topevery.garden.entity.oneMap.MapVo;
import cn.topevery.garden.entity.po.MainPage;
import cn.topevery.garden.entity.query.MainPageQuery;
import cn.topevery.garden.entity.vo.MainPageVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * 
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class MainPageDaoImpl extends BaseDaoImpl<MainPage> implements MainPageDao {
    @Override
    public <P extends DbCsp> List<MainPage> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<MainPageVO> listPage(MainPageQuery query) {
        String table = "t_mainPage tm";

        String columns = "tm.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tm.c_id desc");
        sqlQueryMate
                .where(true, "tm.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, MainPageVO.class);
    }

    @Override
    public MainPage getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public List<CountVO> getCount() {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT\n" +
                "\t  number as value\n" +
                "FROM\n" +
                "\t(\n" +
                "\tSELECT\n" +
                "\t\tCOUNT( * ) number,\n" +
                "\t\t0 id\n" +
                "\tFROM\n" +
                "\t\tt_gre_tree\n" +
                "\tWHERE\n" +
                "\t\tc_db_status = 0 UNION\n" +
                "\tSELECT\n" +
                "\t\tCOUNT( * ) AS number,\n" +
                "\t\tc_famous_type_id id\n" +
                "\tFROM\n" +
                "\t\tt_gre_tree_famous WHERE c_db_status = 0 AND c_famous_type_id is NOT NULL\n" +
                "\tGROUP BY\n" +
                "\t\tc_famous_type_id\n" +
                "\t) t\n" +
                "ORDER BY\n" +
                "\tt.id");

        AtomicReference<List<CountVO>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(CountVO.class));
        });

        return vo.get();
    }

    @Override
    public List<Map> getNumber() {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT\n" +
                "\tc_big_class_name AS name,\n" +
                "\tCONVERT (\n" +
                "\t\tSUM( c_measure )/ 10000,\n" +
                "\tDECIMAL ( 10, 2 )) AS value\n" +
                "\n" +
                "FROM\n" +
                "\tt_gre_land\n" +
                "\twhere c_db_status = 0\n" +
                "GROUP BY\n" +
                "\tc_big_class_name");

        AtomicReference<List<CountVO>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(CountVO.class));
        });
        List<Map> records = new ArrayList<>();
        vo.get().stream().peek(temp -> {
            Map map = new HashMap();
            map.put("value",temp.getValue());
            map.put("name",temp.getName());
            records.add(map);
        }).collect(Collectors.toList());
        return records;
    }

    @Override
    public List<Map> getPlan() {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT\n" +
                "\tCOUNT( c_company_name ) AS planNumber,\n" +
                "\tc_company_name as name ,\n" +
                "\tCOUNT( CASE WHEN t.gg != 0 THEN 1 ELSE NULL END ) AS number\n" +
                "FROM\n" +
                "\t(\n" +
                "\tSELECT\n" +
                "\t\tc_company_name,\n" +
                "\t\tc_plan_id,\n" +
                "\t\tcount( * ) AS zz,\n" +
                "\t\tCOUNT( CASE WHEN c_state = 3 THEN 1 ELSE NULL END ) AS gg\n" +
                "\tFROM\n" +
                "\t\tt_plan\n" +
                "\tWHERE\n" +
                "\t\tc_db_status = 0\n" +
                "\n" +
                "\tGROUP BY\n" +
                "\t\tc_plan_id,\n" +
                "\t\tc_company_name\n" +
                "\t) t\n" +
                "\tWHERE t.c_company_name !='湛江市'\n" +
                "GROUP BY\n" +
                "\tc_company_name\n" +
                "\tUNION\n" +
                "SELECT DISTINCT\n" +
                "\t0,\n" +
                "\tc_dept_short_name ,\n" +
                "\t0\n" +
                "FROM\n" +
                "\tt_sys_dept ts\n" +
                "WHERE\n" +
                "\tts.c_dept_short_name NOT IN ( SELECT c_company_name FROM t_plan )\n" +
                "\tAND c_dept_pid = '03b4ac6d-de58-4dbe-bc73-9a300c44c752'");

        AtomicReference<List<MapVo>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(MapVo.class));
        });
        List<Map> records = new ArrayList<>();
        vo.get().stream().peek(temp -> {
            Map map = new HashMap();
            map.put("number",temp.getNumber());
            map.put("planNumber",temp.getPlanNumber());
            map.put("name",temp.getName());
            records.add(map);
        }).collect(Collectors.toList());
        return records;
    }
}
